<template>
	<view class="content">
		<web-view id="wv" @message="message" :src="src"></web-view>
	</view>


</template>

<script>
	import config from '../../config.js'
	import {
		mapState
	} from 'vuex';
	import {
		pub,sub,unsub
	} from '../../common/event';
	import moment from 'moment/moment';
	export default {
		data() {
			return {
				name: '',
				id: '',
				wv: '',
				src:''
			}
		},
		components: {

		},
		computed: {
			...mapState({
				info:function(state){
					return state.friend.list.find(e=>e.gid == this.id)
				}
			})
		},
		onBackPress(options) {
			console.log(147, options)
			if (options.from && options.from == 'backbutton') {
				return true
			}
			return false
		},
		onUnload() {
			unsub("socket_gaudiocallchange",this.gvideocallchange)
			unsub("socket_gaudiocalluserleave",this.gvideocalluserleave)
			unsub("socket_gaudiocalluserchangeva",this.gvideocalluserchangeva)
		},
		onLoad({
			id,mid
		}) {
			sub("socket_gaudiocallchange",this.gvideocallchange)
			sub("socket_gaudiocalluserleave",this.gvideocalluserleave)
			sub("socket_gaudiocalluserchangeva",this.gvideocalluserchangeva)
			
			
			this.id = id
			this.mid = mid
			this.name = this.info.name
			
			const user = this.$store.state.user.user
			const avatarurl = this.$store.state.common.systemInfo.fileurl + 'avatar/';
			this.src = `/hybrid/html/gaudiocall.html?uid=${user.id}&mid=${mid}&uname=${user.name}&name=${this.name}&id=${id}&ip=${config.ip}&srsUrl=${encodeURIComponent(config.srsUrl)}&avatarurl=${encodeURIComponent(avatarurl)}`
			setTimeout(() => {
				if (!this.wv) {
					let currentWebview = this.$scope.$getAppWebview();
					this.wv = currentWebview.children()[0]
				}
				// this.getdata(mid)
			}, 2000)
		},
		onReady() {
			setTimeout(() => {
				let currentWebview = this.$scope.$getAppWebview();
				this.wv = currentWebview.children()[0]
			}, 500)
		},
		methods: {
			getdata: async function(id){
				const ret = await this.$u.get(`chat/chat/getgaudiocallinfo/${id}`)
				if(ret && ret.success && ret.data){
					this.sendMessage({
						type:"getusers",
						data:ret.data
					})
				}else{
					pub(`socket_videocallchange_${this.mid}`, {
						status:"已完结",
						id:this.mid
					})
					uni.showToast({
						title:"视频通话不存在",
						icon:"none"
					})
					uni.navigateBack()
				}
			},
			gvideocallchange: async function(e){
				console.log(e)
				const user = e.changedata
				this.sendMessage({
					type:"gvideocallchange",
					data:user
				})
			},
			gvideocalluserchangeva: async function(e){
				console.log(e)
				const user = e.changedata
				this.sendMessage({
					type:"gvideocalluserchangeva",
					data:user
				})
			},
			gvideocalluserleave: async function(e){
				console.log(e)
				const user = e.changedata
				this.sendMessage({
					type:"gvideocalluserleave",
					data:user
				})
			},
			changeuseraudio:function(e){
				this.sendMessage({
					type:"changeuseraudio",
					data:e.audio
				})
			},
			changeuservideo:function(e){
				this.sendMessage({
					type:"changeuservideo",
					data:e.video
				})
			},
			handlesingle:function(e){
				this.sendMessage({
					type:"userpubdone",
					data:e
				})
			},
			sendMessage(data) {
				const message = JSON.stringify(data)
				if (!this.wv) {
					let currentWebview = this.$scope.$getAppWebview();
					this.wv = currentWebview.children()[0]
				}
				this.wv.evalJS(`vm.onMessage(${message})`);
			},
			exit:function(){
				this.sendMessage({
					type:"close",
					data:{}
				})
			},
			message: function(e) {
				if(e && e.detail && e.detail.data && e.detail.data.length > 0){
					const user = this.$store.state.user.user
					const message = e.detail.data[0]
					console.log(111,message)
					if(message.type == "pubvideodone"){
						this.$u.post(`chat/chat/groupaudiocall/userpubstream/${this.mid}`)
						this.getdata(this.mid)
					}
					
					if(message.type == "muteaudio"){
						this.$u.put(`chat/chat/groupaudiocall/changeuserva/${this.mid}`,{
							id:this.$store.state.user.user.id,
							audio:message.data,
						})
					}
					
					if(message.type == "mutevideo"){
						this.$u.put(`chat/chat/groupaudiocall/changeuserva/${this.mid}`,{
							id:this.$store.state.user.user.id,
							video:message.data,
						})
					}
					
					if(message.type == "exit"){
						this.$u.put(`chat/chat/gaudiocall/userleave/${this.mid}`)
						uni.navigateBack()
					}
					
					if(message.type == "closedone"){
						
						uni.navigateBack()
					}
					
				}
			},
		}
	}
</script>


<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100vh;
		overflow-y: hidden;
		padding-bottom: 120rpx;
		border-start-end-radius: 32rpx;
		border-start-start-radius: 32rpx;
	}
</style>